package ManagersArea;

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import module4.*;
import java.util.Vector;

public class AdvancedSettingsDAO
{
	private Vector outVect = null;
	
	//**connection Variables
	private Connection turtleConn;
	//*****************************************
	
	public AdvancedSettingsDAO(Connection inConn)//correct Spelling
	{
		turtleConn = inConn;
	}//end constructor
	
	public Vector populatePriceVect(int selected,JLabel nameLabel ,JLabel oldPriceLabel)
	{
		try
		{
			Statement priceTableStmt = turtleConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			ResultSet priceTableRS;
			String priceTableQuery = "SELECT catName,catPrice FROM priceCatTable";
			priceTableRS = priceTableStmt.executeQuery(priceTableQuery);
			String tempSTR;
			priceTableRS.last();
			int amtRows = priceTableRS.getRow();
			double oldPrice;
			priceTableRS.first();
			outVect = new Vector();
			for(int i = 1;i <= amtRows;i++ )
			{
				tempSTR = priceTableRS.getString(1);
				oldPrice = priceTableRS.getDouble(2);
	
				System.out.println(""+tempSTR);
				outVect.addElement(tempSTR);
				if(i == selected)
				{
					nameLabel.setText(tempSTR);
					oldPriceLabel.setText("Current Price : R"+ oldPrice);
				}
				priceTableRS.next();
			}
		}
		catch(SQLException sqlE)
		{
			System.out.println("No problemo");
			sqlE.printStackTrace();
		}//end catch	
		return outVect;
	}//end populatePrice VECTOR
	
	public void setCatPrice(String catName,String amountPrice)
	{
		try
		{
			int rowsAffected;
			double newPrice = Double.parseDouble(amountPrice);
			Statement setPriceSTMT = turtleConn.createStatement();
			String updatePriceQuery = "UPDATE priceCatTable SET catPrice =" + newPrice + "WHERE catName = '"+catName+"'";
			rowsAffected = setPriceSTMT.executeUpdate(updatePriceQuery);
			System.out.println("Rows Affected :" + rowsAffected);
		}
		catch(SQLException sqlE)
		{
			sqlE.printStackTrace();
		}	
		
	}//end setCatPrice
	
	public boolean addNewStockType(String id,String name,int mORh,String depValue)
	{
		boolean idCheck = false;
		try
		{
			String uniqueId;
			//getting unique stockTypeId
			ResultSet idCheckRS;
			Statement idCheckSTMT = turtleConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			String idCheckQUERY = "SELECT stockTypeId FROM stockTypeTable";
			idCheckRS = idCheckSTMT.executeQuery(idCheckQUERY);
			//first check users choice
			int amtRows;
			idCheckRS.last();
			amtRows = idCheckRS.getRow();
			System.out.println("Amount of rows : " + amtRows);
			idCheckRS.first();
			for(int i = 1;i <= amtRows;i++)
			{
				if(idCheck == true)
				break;
				System.out.println("no1 : " +id+" no2 : " + idCheckRS.getString(1));
				if(id.equals(idCheckRS.getString(1)))
				idCheck = true;
				
				idCheckRS.next();
			}//end 1st check
			
			if (idCheck == false)
			uniqueId = id;
			else 
			{
				idCheckRS.last();
				int tempInt = Integer.parseInt(idCheckRS.getString(1));
				tempInt++;
				uniqueId = new String(""+tempInt);
			}
				
			//end id Checker
			Statement addStockTypeSTMT = turtleConn.createStatement();
			int rowsAffected;
			String type;
			if(mORh == 0)
			type = "Media";
			else
			type = "Hardware";
			
			String addNewSITypeQUERY = "INSERT INTO stockTypeTable VALUES('"+uniqueId +"','"+name+"','"+type+"',"+depValue+")";
			System.out.println(addNewSITypeQUERY);
		
			rowsAffected = addStockTypeSTMT.executeUpdate(addNewSITypeQUERY);
			System.out.println("RowsAffected : " + rowsAffected);
		}
		catch(SQLException sqlE)
		{
			sqlE.printStackTrace();
		}	
		return idCheck;
	}
	
	public boolean addNewPriceCat(String id,String name,double price)
	{
		boolean idCheck = false;
		try
		{
			String uniqueId;
			//getting unique PriceCatID
			ResultSet idCheckRS;
			Statement idCheckSTMT = turtleConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			String idCheckQUERY = "SELECT catId FROM priceCatTable";
			idCheckRS = idCheckSTMT.executeQuery(idCheckQUERY);
			//first check users choice
			int amtRows;
			idCheckRS.last();
			amtRows = idCheckRS.getRow();
			System.out.println("Amount of rows : " + amtRows);
			idCheckRS.first();
			for(int i = 1;i <= amtRows;i++)
			{
				if(idCheck == true)
				break;
				System.out.println("no1 : " +id+" no2 : " + idCheckRS.getString(1));
				if(id.equals(idCheckRS.getString(1)))
				idCheck = true;
				
				idCheckRS.next();
			}//end 1st check
			
			if (idCheck == false)
			uniqueId = id;
			else 
			{
				idCheckRS.last();
				int tempInt = Integer.parseInt(idCheckRS.getString(1));
				tempInt++;
				uniqueId = new String(""+tempInt);
			}	
			
			Statement addPriceCatSTMT = turtleConn.createStatement();
			int rowsAffected;
			
			String addNewPriceCatQuery = "INSERT INTO priceCatTable VALUES('"+uniqueId +"','"+name+"',"+price+")";
			System.out.println(addNewPriceCatQuery);
		
			rowsAffected = addPriceCatSTMT.executeUpdate(addNewPriceCatQuery);
			System.out.println("RowsAffected : " + rowsAffected);
		}
		catch(SQLException sqlE)
		{
			sqlE.printStackTrace();
		}	
		return idCheck;
	}
}//end class declaration